package com.jhhc.IoUtils;

import com.jhhc.RHDB.Data.Pojo.ST_PPTN_R;
import com.jhhc.RHDB.Data.Pojo.ST_RIVER_R;
import com.jhhc.RHDB.Data.Pojo.ST_RSVR_R;

import java.io.File;
import java.io.RandomAccessFile;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class DataGather {

    private   String path = "";

    public   String openFileStyle = "r";

    public   String fieldLimitChar = ",";

    public   int fieldAllCount = 7;

    private int count;

//    private ST_PPTN_R st_pptn_r;
//    private ST_RIVER_R st_river_r;
//    private ST_RSVR_R st_rsvr_r;



    //遍历文件
    public static List<String> fileTest(String fl) throws Exception {
        //System.out.println("AAAA");
        File file = new File(fl);
        String[] files = file.list();
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            File file1 = new File(fl, files[i]);
            strings.add(fl + "\\" + file1.getName());// 添加文件全路径名
        }

        List<String> fileList = new ArrayList<>();
        for (int i = 0; i < strings.size(); i++) {
            File file1 = new File(strings.get(i));
            String[] F = file1.list();
            for (int j = 0; j < F.length; j++) {
                File file2 = new File(strings.get(i), F[j]);
                fileList.add(strings.get(i) + "\\" + file2.getName());// 添加文件全路径名
            }
        }
        Predicate<String> contain = n -> n.contains("ST");
        List<String> stringList = fileList.stream().filter(contain).collect(Collectors.toList());
//        for (int i = 0; i < stringList.size(); i++) {
//            utils utils = new utils();
//            List<ST_PPTN_R> ST_PPTN_R = utils.Read(stringList.get(i));
//        }
        System.out.println(stringList);
        return stringList;

    }
    /*

     * 功能：解析文本文件

     */

//    public void loadFile() {
//
//
//
//
//        try {
//
//
//
//               RandomAccessFile raf = new RandomAccessFile(path, openFileStyle);
//
//                String line_record = raf.readLine();
//                tranStr(line_record);
//                while (line_record != null) {
//
//                    // 解析每一条记录
//                    parseRecord(line_record);
//
//                    line_record = raf.readLine();
//                    if (line_record +""=="")continue;
//                    System.out.println(line_record);
//                    parseRecord(line_record);
//                }
//
//
//
//            System.out.println("共有合法的记录" + count + "条");
//
//        } catch (Exception e) {
//
//            e.printStackTrace();
//
//        }
//
//    }



    /*

     * 功能：具体解析每一条记录，这里可以增加很多对记录的解析判断条件，如是否为字母、

     * 数字、email等。

     */

    public void parseRecord() throws Exception {
        //拆分记录


        String[] fields =null;
        RandomAccessFile raf =null;
        InsertDB db = new InsertDB();
        List<String> list = DataGather.fileTest("C:\\Users\\Administrator\\Desktop\\txt");
        for (int i=0;i<list.size();i++) {
            System.out.println(list.size());


            String name = list.get(i).substring(39, 52);
            switch (name) {
                case "ST_PPTN_R.txt":
                    path = name;
                    fields = path.split(fieldLimitChar);//根据,分割字段
                    raf = new RandomAccessFile("C:\\Users\\Administrator\\Desktop\\txt\\txt\\"+path, openFileStyle);
                    String line_record = raf.readLine();
                    tranStr(line_record);
                    while (line_record != null) {
                        ST_PPTN_R st_pptn_r =new ST_PPTN_R();
                        line_record = raf.readLine();
                        if (line_record +""=="")continue;

                            st_pptn_r.setStcd(tranStr(fields[0]));
                            st_pptn_r.setTm(tranStr(fields[1]));
                            st_pptn_r.setDrp(Double.valueOf(tranStr(fields[2])));
                            st_pptn_r.setIntv(Double.valueOf(tranStr(fields[3])));
                            st_pptn_r.setPdr(Double.valueOf(tranStr(fields[4])));
                            st_pptn_r.setDyp(Double.valueOf(tranStr(fields[5])));
                            st_pptn_r.setWth(tranStr(fields[6]));
                            boolean a = db.insertDBPPTN(st_pptn_r);

                            System.out.println(st_pptn_r);


                        System.out.println(1);
                    }


                    break;
                case "ST_RIVER_R.tx":
                    path = "ST_RIVER_R.txt";
                    fields = path.split(fieldLimitChar);//根据,分割字段
                    raf = new RandomAccessFile(path, openFileStyle);


                    break;
                case "ST_RSVR_R.txt":
                    path = name;
                    fields = path.split(fieldLimitChar);//根据,分割字段
                    raf = new RandomAccessFile(path, openFileStyle);
                    break;
                default:
                    break;
            }

        }


            System.out.println("chaifen");


            count++;



    }



    private String tranStr(String oldstr) {

        String newstr = "";

        try {

            newstr = new String(oldstr.getBytes("ISO-8859-1"), "GBK");

        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();

        }

        return newstr;

    }

}

